package com.smsc.headend.task.engine.aop;


import cn.hutool.core.date.SystemClock;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class KafkaAspect {

    @Pointcut("@annotation(org.springframework.kafka.annotation.KafkaListener)")
    public void kafkaLog() {

    }

    @Around("kafkaLog()")
    public void interceptKafkaListener(ProceedingJoinPoint joinPoint) throws Throwable {

        Long start = SystemClock.now();
        try {
            joinPoint.proceed();
        } catch (Exception e) {
            log.error("Exception in kafka message: ", e);
        }
        Long cost = SystemClock.now() - start;
        if (cost > 5000) {
            log.warn("time to consume message too long {}ms, methodName={}", cost, joinPoint.getSignature().getName());
        }
    }
}
